package com.yf.service.im.impl;


import com.yf.dao.im.FriendDao;
import com.yf.dao.im.GroupUserDao;
import com.yf.dao.im.MsgGroupDao;
import com.yf.entity.*;
import com.yf.enums.ChatTypeEnum;
import com.yf.im.protocol.MsgPb;
import com.yf.service.im.MsgService;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MsgServiceImpl extends BaseServiceImpl<MsgEntity> implements MsgService {

    @Autowired
    private MsgGroupDao msgGroupDao;


    @Autowired
    private FriendDao friendDao;

    @Autowired
    private GroupUserDao groupUserDao;

    @Override
    public List<MsgEntity> offlineMsgList(Long userId) {
        return null;
    }

    @Override
    public MsgEntity saveMsg(MsgPb.Message msgEntity) {
        MsgEntity msg = protoToMsg(msgEntity);
        save(msg);
        return msg;
    }


    @Override
    public  MsgPb.Message msgToProto(Object msgEntity) {
       MsgPb.Message.Builder build =MsgPb.Message.newBuilder();
        BeanUtil.copy(msgEntity,build);
        return build.build();
    }

    @Override
    public  MsgEntity protoToMsg(Object msgEntity) {
        MsgEntity msgEntity1 = new MsgEntity();
        BeanUtil.copy(msgEntity,msgEntity1);
        return msgEntity1;
    }

    @Override
    public List<MsgEntity> getMsgRecord(Long userId) {
        //先查询聊天的好友
//        List<Long> friendList = msgGroupDao.getChatFriend(userId,0);
        //查询加入的群
//        List<Long> groupList = msgGroupDao.getChatFriend(userId,1);

//        List list = new ArrayList();
        //查询聊天记录
//        if(!friendList.isEmpty()){
//            for (Long fid : friendList) {
//                MsgEntity msgEntity = msgGroupDao.getLatestRecord(userId,fid);  //最新的一条记录
//                List<MsgEntity> msgList = msgGroupDao.getChatRecord(userId,fid); //所有记录
//                Map msgMap = new HashMap();
//                msgMap.put("nowmsg",msgEntity);
//                msgMap.put("msgarray",msgList);
//                map.put(fid.toString(),msgMap);
//            }
//        }
//
//        //查询加入的群聊天记录
//        if(!groupList.isEmpty()){
//            for (Long gid : groupList) {
//                MsgEntity msgEntity =  msgGroupDao.getLatestGroupRecord(gid); //查询群聊最新记录
//                Map map1 = new HashMap();
//                map1.put("toUserId",gid);
//                List<MsgEntity> list = msgDao.queryList(map1); //查询群聊全部记录
//                Map msgMap = new HashMap();
//                msgMap.put("nowmsg",msgEntity);
//                msgMap.put("msgarray",list);
//                map.put(gid.toString(),msgMap);
//            }
//
//
//        }

//

        return msgGroupDao.getChatRecord(userId);

    }

    @Override
    public List<MsgEntity> getFriendMsg(UserEntity userEntity, Long sessionId,Integer chatType,Long msgTime,Integer limit) {
        List<MsgEntity> list = new ArrayList<>();
        boolean flag = false;
        //先验证关系
        if(ChatTypeEnum.SINGLECHAT.getState()==chatType){
            //验证是否是好友
            FriendEntity friendEntity = friendDao.isFriend(userEntity.getId(),sessionId);
            if(friendEntity!=null){
                flag = true;
            }

        }else if(ChatTypeEnum.GROUPCHAT.getState()==chatType){
            //判断有没有加群
            GroupUserEntity group = groupUserDao.findByGroupIdAndUserId(userEntity.getId(),sessionId);
            if(group!=null){
                flag = true;
            }

        }

        //查询聊天记录
        if(flag){
            list = msgGroupDao.getFriendChatRecord(userEntity.getId(),sessionId,chatType,msgTime,limit);
        }

        return list;
    }
}
